#include "UserService.h"
#include <QDebug>

UserService::UserService()
{

}

/* 通过id查找用户 */
User *UserService::findUserById(const quint32 &id)
{
	return ud.findOne(id);
}

/* 通过username查找用户 */
User *UserService::findUserByUsername(const QString &username)
{
	QLsSpecification spec;
	spec.setSqlWhere(
			QLsSpecification::equal(
				"username",
				QLsSpecification::tranString(username)
				)
			);

	return ud.findOne(spec);
}

/* 通过username和password查找用户 */
User *UserService::findUserByUsernameAndPassword(const QString &username, const QString &password)
{
	QLsSpecification spec;
	spec.setSqlWhere(
			QLsSpecification::And(
				QLsSpecification::equal(
					"username",
					QLsSpecification::tranString(username)
					),
				QLsSpecification::equal(
					"password",
					QLsSpecification::tranString(password)
					)
				)
			);

	return ud.findOne(spec);
}

/* 将user存入数据库 */
quint32 UserService::addUser(const User &user)
{
	QLsSpecification spec;
	User *u_tmp;

	/* 校验username */
	spec.setSqlWhere(
			QLsSpecification::equal(
				"username",
				QLsSpecification::tranString(user.getUsername())
				)
			);
	if(ud.findOne(spec))
	{
		printf("该用户已存在。\n");
		return UserType::ERRNO_ALREADYEXIST;
	}

	/* 校验mobile */
	spec.setSqlWhere(
			QLsSpecification::equal(
				"mobile",
				QLsSpecification::tranString(user.getMobile())
				)
			);
	if(ud.findOne(spec))
	{
		printf("该手机号已被绑定。\n");
		return UserType::ERRNO_MOBILE_ALREADYEXIST;
	}

	/* 校验邮箱 */
	spec.setSqlWhere(
			QLsSpecification::equal(
				"email",
				QLsSpecification::tranString(user.getEmail())
				)
			);
	u_tmp = ud.findOne(spec);
	if(u_tmp && u_tmp->getEmail() != "")
	{
		printf("该邮箱已被绑定。\n");
		return UserType::ERRNO_EMAIL_ALREADYEXIST;
	}

	ud.save(&user);
	return UserType::ERRNO_SUCCESS;
}

/* 修改用户数据 */
quint32 UserService::updateUser(const User &user)
{
	QLsSpecification spec;
	User *u_tmp;

	/* 校验username */
	spec.setSqlWhere(
			QLsSpecification::equal(
				"username",
				QLsSpecification::tranString(user.getUsername())
				)
			);
	if(!ud.findOne(spec))
	{
		printf("该用户不存在。\n");
		return UserType::ERRNO_INEXISTENCE;
	}

	/* 校验mobile */
	spec.setSqlWhere(
			QLsSpecification::And(
				QLsSpecification::equal(
					"mobile",
					QLsSpecification::tranString(user.getMobile())
					),
				QLsSpecification::notEqual(
					"id",
					QString::number(user.getId())
					)
				)
			);
	if(ud.findOne(spec))
	{
		printf("该手机号已被绑定。\n");
		return UserType::ERRNO_MOBILE_ALREADYEXIST;
	}

	/* 校验邮箱 */
	spec.setSqlWhere(
			QLsSpecification::And(
				QLsSpecification::equal(
					"email",
					QLsSpecification::tranString(user.getEmail())
					),
				QLsSpecification::notEqual(
					"id",
					QString::number(user.getId())
					)
				)
			);
	u_tmp = ud.findOne(spec);
	if(u_tmp && u_tmp->getEmail() != "")
	{
		printf("该邮箱已被绑定。\n");
		return UserType::ERRNO_EMAIL_ALREADYEXIST;
	}

        ud.save(&user);
	return UserType::ERRNO_SUCCESS;
}

/* 查询表是否存在 */
bool UserService::exists(const QString &databaseType)
{
	return ud.exists(databaseType);
}

/* 创建表 */
void UserService::CreateTable(const QString &databaseType)
{
	return ud.CreateTable(databaseType);
}

/* 通过username,password,mobile查找用户 */
User *UserService::findUserByUsernameAndPwdAndMobile(
		const QString &username, const QString &password, const QString &mobile)
{
	QLsSpecification spec;

	QList<QString> list;
	list.push_back(
			QLsSpecification::equal(
				"username",
				QLsSpecification::tranString(username)
				)
			);
	list.push_back(
			QLsSpecification::equal(
				"password",
				QLsSpecification::tranString(password)
				)
			);
	list.push_back(
			QLsSpecification::equal(
				"mobile",
				QLsSpecification::tranString(mobile)
				)
			);
	spec.setSqlWhere(QLsSpecification::And(list));

	return ud.findOne(spec);
}

/* 通过username,mobile查找用户 */
User *UserService::findUserByUsernameAndMobile(const QString &username, const QString &mobile)
{
	QLsSpecification spec;

	QList<QString> QList;
	QList.push_back(
			QLsSpecification::equal(
				"username",
				QLsSpecification::tranString(username)
				)
			);
	QList.push_back(
			QLsSpecification::equal(
				"mobile",
				QLsSpecification::tranString(mobile)
				)
			);
	spec.setSqlWhere(QLsSpecification::And(QList));

	return ud.findOne(spec);
}

